import { Component, Injector, OnInit, ViewChild } from "@angular/core";
import { ActivatedRoute, Router } from "@angular/router";
import { LazyLoadEvent, Paginator } from "primeng/primeng";
import {
    DeviceServiceProxy,
    DeviceList,
} from "@shared/service-proxies/service-proxies";
import { AppComponentBase } from "@shared/common/app-component-base";
import { Table } from "primeng/table";
import { finalize } from "rxjs/operators";
import { appModuleAnimation } from "@shared/animations/routerTransition";
@Component({
    selector: "app-devices",
    templateUrl: "./devices.component.html",
    styleUrls: ["./devices.component.css"],
    animations: [appModuleAnimation()],
})
export class DevicesComponent extends AppComponentBase implements OnInit {
    @ViewChild("dataTable") dataTable: Table;
    @ViewChild("paginator") paginator: Paginator;
    constructor(
        injector: Injector,
        private route: Router,
        private activeRoute: ActivatedRoute,
        private _deviceServiceProxy: DeviceServiceProxy
    ) {
        super(injector);
    }
    parkingid: number;
    parkingName: string;
    parkingCoce: string;
    neighbourhood_Name: string;
    neighbourhood_id: number;
    guardId: number;
    guardNo: string;
    guradName: string;

    ngOnInit() {
        this.activeRoute.queryParams.subscribe((params) => {
            this.parkingid = params["parkingid"];
            this.parkingName = params["parkingName"];
            this.parkingCoce = params["parkingCoce"];
            this.neighbourhood_Name = params["neighbourhood_Name"];
            this.neighbourhood_id = params["neighbourhood_id"];
            this.guardId = params["prakGruardId"];
            this.guardNo = params["prakGruardCode"];
            this.guradName = params["prakGruardName"];
        });
    }
    getDevice(event?: LazyLoadEvent): void {
        this._deviceServiceProxy
            .getAll(
                this.parkingid,
                this.guardId,
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getSkipCount(this.paginator, event),
                this.primengTableHelper.getMaxResultCount(this.paginator, event)
            )
            .pipe(
                finalize(() => this.primengTableHelper.hideLoadingIndicator())
            )
            .subscribe((result) => {
                console.log(result);
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.totalRecordsCount = result.items.length;
                this.primengTableHelper.hideLoadingIndicator();
                console.log(result.items);
            });
    }
    deleteDevice(Device:DeviceList ):void{
      this.message.confirm(
        this.l("删除设备" + Device.deviceName + "?"),
        this.l("AreYouSure"),
        (isConfirmed) => {
            if (isConfirmed) {
                this._deviceServiceProxy
                    .delete(Device.id)
                    .subscribe(() => {
                        this.getDevice();
                        abp.notify.success(this.l("SuccessfullyDeleted"));
                    });
            }
        }
    );
    }
}
